package com.softserveinc.controller;

/**
 * Controller for login page
 */

import com.google.gdata.client.authn.oauth.OAuthException;
import com.google.gdata.client.calendar.CalendarService;
import com.google.gdata.data.calendar.CalendarEntry;
import com.google.gdata.data.calendar.CalendarFeed;
import com.google.gdata.util.ServiceException;
import com.softserveinc.dao.IAuthoritiesDAO;
import com.softserveinc.dao.impl.UserDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.io.IOException;
import java.net.URL;

@Controller
public class LoginController {
    @Autowired
    IAuthoritiesDAO authoritiesDAO;

    @Autowired
    UserDAO userDAO;


    /**
     * Method realize request mapping for login page
     *
     * @return - login.jsp
     */
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login(Model model) throws OAuthException, IOException, ServiceException {
//        final String CLIENT_ID = "527281763994-bheoihoq081ot9t8f82i2hbk6ikm23bt.apps.googleusercontent.com";
//        final String CLIENT_SECRET = "rCnXFfV6lxAUCJgS05eeweOm";
//        GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters();
//        oauthParameters.setOAuthConsumerKey(CLIENT_ID);
//        oauthParameters.setOAuthConsumerSecret(CLIENT_SECRET);
//        oauthParameters.setScope("https://www.googleapis.com/auth/calendar");
//        oauthParameters.setOAuthCallback("http://localhost:8080/login");
//        oauthParameters.setOAuthType(OAuthParameters.OAuthType.TWO_LEGGED_OAUTH);


//        CalendarService service = new CalendarService("Example");
//        service.setOAuthCredentials(oauthParameters, new OAuthHmacSha1Signer());
        //Create a CalenderService and authenticate
         CalendarService service = new CalendarService("exampleCo-exampleApp-1");
//        service.setUserCredentials("eosylm@gmail.com", "Blitzkrieg1945");

        String user = "eosylm@gmail.com";
        URL feedUrl = new URL("https://www.google.com/calendar/feeds/eosylm%40gmail.com/private-20185c3f247656fac836a74c95e94ca5/basic" +
                "?xoauth_requestor_id=" + user);

        CalendarFeed resultFeed = service.getFeed(feedUrl, CalendarFeed.class);

        String result = "";
        for (int i = 0; i < resultFeed.getEntries().size(); i++) {
            CalendarEntry entry = resultFeed.getEntries().get(i);
            result += entry.getTitle().getPlainText();
        }


        model.addAttribute("result", result);


        return "login";

    }

    /**
     * Method realize request mapping for error page
     *
     * @return - login.jsp
     */
    @RequestMapping(value = "/error", method = RequestMethod.GET)
    public String error() {

        return "error";

    }

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home() {

        return "login";

    }
}
